#include "cybertron/sensor/noise/PeriodicTimeNoise.hpp"
#include  "cybertron/sensor/noise/WhiteNoise.hpp"
#include <algorithm>

CYBERTRON_BEGIN

PeriodicTimeNoise::PeriodicTimeNoise(double horizontalAmplitude/* = 1.0*/, double verticalAmplitude/* = 0.6*/, double period/* = 3600 * 24 * 365*/)
	: TimeNoise(EPeriodicTimeNoise), m_horizontalAmplitude(horizontalAmplitude), m_verticalAmplitude(verticalAmplitude), m_period(period)
{
	m_period = std::max(m_period, 1e-3);
	std::uniform_real_distribution<float> distribution(0, (float)NOISE_PI);
	m_initialPhase = vec3(distribution(randomEngine()), distribution(randomEngine()), distribution(randomEngine()));
}

PeriodicTimeNoise::~PeriodicTimeNoise()
{
}

vec3 PeriodicTimeNoise::update() {
	double seconds = elapsedTime();
	double omega = NOISE_PI * 2 / m_period;
	float x = (float)(m_horizontalAmplitude * std::cos(omega * seconds + m_initialPhase.x));
	float y = (float)(m_horizontalAmplitude * std::cos(omega * seconds + m_initialPhase.y));
	float z = (float)(m_verticalAmplitude * std::cos(omega * seconds + m_initialPhase.z));
	m_value = vec3(x, y, z);
	return m_value;
}

CYBERTRON_END